package com.smart.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.smart.domain.User;

@Repository
public class UserDao {
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	private final static String MATCH_COUNT_SQL="SELECT * FROM user WHERE username=? ";
	

	public int getMatchCount(String userName, String password) {
		String sqlStr="SELECT  count(*) FROM user "+ "WHERE username=? and password=? ";
		int i=jdbcTemplate.queryForObject(sqlStr,Integer.class,new Object[]{ userName, password }).intValue();
		System.out.println(sqlStr);
		return i;
		
	}
	
	
	
	
	public User findUserByUserName(final String userName) {
		final User user=new User();
		jdbcTemplate.query(MATCH_COUNT_SQL, new Object[]{userName},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException{
				//user.setUserId(rs.getInt("userid"));
				user.setUserName(rs.getString("username"));
			}
		});
		
		return user;
	}
}
